package MTRequest;

import android.database.Cursor;
import MTClient.MessageCentre;
import MTClient.MTConstant;
import SQLManager.SQLManager;

/**
 * This Request is handling the client to register a new account
 * @author King
 *
 */
public class MTClientRegisterRequest extends MTRequest {
	
	
	public MTClientRegisterRequest( MessageCentre aMSGCenter )
	{
		super( aMSGCenter );
		this.mRequestState = MTConstant.MT_REQ_CREG_SEND_PHONENUM;
	}
	
	protected void UpdateRequestState( int aPeerState )
	{
		if( MTConstant.MT_REQ_FINISH != this.mRequestState ){
			if( MTConstant.MT_REQ_SREG_UPDATE_USER_INFO != aPeerState ){
				this.mRequestState = aPeerState + 1;
			}
			else{
				this.mRequestState = MTConstant.MT_REQ_FINISH;
			}
		}
	}
	
	protected void ProcessReqest( int aPeerState, String peerRawData )
	{
		switch( aPeerState ){
			case MTConstant.MT_REQ_SREG_CHECK_PHONENUM:{
				System.out.println("Client: MTConstant.MT_REQ_SREG_CHECK_PHONENUM ");
				this.mReceivedFromPeerRawData.add( peerRawData );
				
				// Check phone number available;
				
				boolean isAvailabe = Boolean.valueOf( peerRawData );
				
				if( false == isAvailabe ){
					this.mRequestDataSource.Notify( 
							MTConstant.MT_REQID_REGISTER,
							this.mRequestState,
							null );
					this.mRequestState = MTConstant.MT_REQ_FINISH;
				}
				break;
			}
			case MTConstant.MT_REQ_SREG_UPDATE_USER_INFO:{
				System.out.println("Client: MTConstant.MT_REQ_SREG_UPDATE_USER_INFO ");
				this.mReceivedFromPeerRawData.add( peerRawData );
				
				int userID = Integer.valueOf( peerRawData );

				
				this.mRequestDataSource.Notify( 
					MTConstant.MT_REQID_REGISTER,
					this.mRequestState,
					peerRawData );
				
				break;
			}
		}
	}
	
	public void SendRequest()
	{
		String request = "" + MTConstant.MT_REQID_REGISTER + MTConstant.MT_REQ_ITEM_SEPARATER + 
		this.mRequestState + MTConstant.MT_REQ_ITEM_SEPARATER;
		switch( this.mRequestState ){
			case MTConstant.MT_REQ_CREG_SEND_PHONENUM:{
				String phoneNum = this.mRequestDataSource.GetRawData();
				//int phoneNum = 123456789;
				request += phoneNum;
				break;
			}
			case MTConstant.MT_REQ_CREG_SEND_USER_INFO:{
				String userName = this.mRequestDataSource.GetRawData();
				if( userName.matches("") ){
					userName = " ";
				}
				//String userName = "User3";
				//
				// ....
				
				//Send User Information to Server;
				request += userName;
				break;
			}
			case MTConstant.MT_REQ_FINISH:{
				this.Finish();
				return;
			}
		}
		this.SendMSG(request);
	}
}
